package app.vendorportal.service;

import app.vendorportal.utils.DateDetail;

import core.framework.util.DateUtils;

import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author hubery.chen
 */
@Service
public class DataConverter {

    public String getDateByDateTime(Date date) {
        return Integer.toString(DateUtils.getYear(date)) + handleDateTime(DateUtils.getMonth(date) + 1) + handleDateTime(DateUtils.getDay(date));
    }

    public String getTimeByDateTime(Date date) {
        return handleDateTime(DateUtils.getHour(date)) + handleDateTime(DateUtils.getMinute(date));
    }

    public String constructInterchangeControlNumberByHistoryId(int historyId) {
        return String.format("%09d", historyId);
    }

    private String handleDateTime(int value) {
        if (value < 10) {
            return "0" + Integer.toString(value);
        }
        return Integer.toString(value);
    }

    public DateDetail constructDateDetail(Date date, String format) {
        DateDetail dateDetail = new DateDetail();
        dateDetail.setFormat(format);
        dateDetail.setDate(date);
        return dateDetail;
    }

}
